﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Blizzard.Contracts;
using Blizzard.Mapper;
using System.Reflection;

namespace Blizzard.Scripts
{
  public class DDLScript
    {
        private Type[] _types = null;
   
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="args">Array of all bussiness data types as a base for generating sql script</param>
        public DDLScript( params Type[] args)
        {
           _types = args;
        }

        /// <summary>
        /// Generates CREATE TABLE scripts
        /// </summary>
        /// <param name="syntax"></param>
        /// <returns></returns>

        public string GenerateScript(IMapper mapper, IDDLSyntax syntax)
        {

            List<Table> tables = new List<Table>();

            foreach(Type type in _types)
            {
                Table table = new Table(mapper.GetTableName(type));
                tables.Add(table);

                foreach (PropertyInfo pi in type.GetProperties())
                {

                    if (Helper.IsAList(pi.PropertyType))
                    {//L1
                        string jointableName = mapper.GetJoinTableName(pi);
                        Table joinTable = null;
                        bool isAlreadyInTheList = false;
                        foreach (Table t in tables)
                        {
                            if (t.Name == jointableName)
                            {//joinTable already exists in tables lists so we dont need to create it and added to the list
                                Table.Field field = new Table.Field(mapper.GetMyIdFieldName(pi), typeof(System.Int32));
                                t.Fields.Add(field);
                                isAlreadyInTheList = true;
                                break;
                            }
 
                        }

                        if (!isAlreadyInTheList)
                        {
                            if (!String.IsNullOrEmpty(jointableName))
                            {
                                joinTable = new Table(jointableName);
                                joinTable.IdentityFieldName = "Id";
                                joinTable.Fields.Add(new Table.Field(mapper.GetMyIdFieldName(pi), typeof(System.Int32)));

                                tables.Add(joinTable);
                            }
                        }
                    }
                    else if (mapper.IsIdentityColumn(pi))
                    {//M1
                        table.IdentityFieldName = mapper.GetIdentityFieldName(type);
                    }
                    else
                    {//R1
                        if (Helper.ImplementsIDataObjects(pi.PropertyType))
                        {//when we are here, we are dealing with a field that associates with a parent
                            // so this must be an parentId field therefore of type int.
                            table.Fields.Add(new Table.Field(mapper.GetFieldName(pi), typeof(System.Int32)));
                        }
                        else
                        {
                            table.Fields.Add(new Table.Field(mapper.GetFieldName(pi), pi.PropertyType));
                        }
                    }

                }
            }

            return syntax.CreateScript(tables);
        }


    

     
         

    }
}
